package com.upup.train_management.controller;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.servlet.http.HttpServletRequest;

@Controller
public class CustomErrorController implements ErrorController {

    @RequestMapping("/error")
    public String handleError(HttpServletRequest request, Model model) {
        Object statusCode = request.getAttribute("jakarta.servlet.error.status_code");
        Object errorMessage = request.getAttribute("jakarta.servlet.error.message");
        Object requestUri = request.getAttribute("jakarta.servlet.error.request_uri");

        model.addAttribute("statusCode", statusCode != null ? statusCode : 500);
        model.addAttribute("errorMessage", errorMessage != null ? errorMessage.toString() : "内部服务器错误");
        model.addAttribute("requestUri", requestUri != null ? requestUri.toString() : "未知");

        return "error";
    }
}